<%--
Input:
		(String) FlexBlobId			Blob id with the value of the flex attribute(var) (blobvalue in Mungo table)
		(String) mimeType			
		(String) FlexBlobOutputVar	Variable name to store the result (var)
Output:
		(String) Variable name    	String with the name of the "FlexBlobOutputVar" input parameter (var). Contains the Blob URL
		(String) fileName
Updates:

--%><%@ taglib prefix="cs" uri="futuretense_cs/ftcs1_0.tld" %><%@ 
taglib prefix="render" uri="futuretense_cs/render.tld" %><%@ 
taglib prefix="blobservice" uri="futuretense_cs/blobservice.tld" %><%@ 
taglib prefix="satellite" uri="futuretense_cs/satellite.tld" %><%@ 
taglib prefix="ics" uri="futuretense_cs/ics.tld" %><%@ 
page import="COM.FutureTense.Interfaces.IList" %><%@ 
page import="COM.FutureTense.Interfaces.Utilities" %><cs:ftcs><ics:if 
condition='<%=ics.GetVar("seid")!=null%>'><ics:then><render:logdep 
	cid='<%=ics.GetVar("seid")%>' c="SiteEntry"/></ics:then></ics:if><ics:if 
condition='<%=ics.GetVar("eid")!=null%>'><ics:then><render:logdep cid='<%=ics.GetVar("eid")%>' c="CSElement"/></ics:then></ics:if><%
int _STRINGBUFFER_SIZE_ = 200;
String ERR_VAR = "Error";
String EMPTY_STRING = "";

String site = ics.GetVar("site");

String blobFieldId = ics.GetVar ("FlexBlobId");
String preferredFilename = ics.GetVar("PREFERREDFILE");
String preferredDir = ics.GetVar("PREFERREDDIR");
String mimeType = ics.GetVar("mimeType");
String outputVar = ics.GetVar ("FlexBlobOutputVar");


String fileName = null;
String extension = null;



if ( Utilities.goodString( blobFieldId ) && Utilities.goodString( outputVar ) ){
	%><blobservice:readdata id='<%=blobFieldId%>' listvarname="dataList"/><%		

	StringBuffer errStr = null;

	IList dataList = ics.GetList("dataList");
	String stWhere = dataList.getValue("urldata");
	if (stWhere.lastIndexOf(".") != -1){
		extension = stWhere.substring(stWhere.lastIndexOf(".") + 1, stWhere.length());	
		fileName = stWhere.substring(stWhere.lastIndexOf("/") + 1, stWhere.lastIndexOf("."));	

		if (!Utilities.goodString(mimeType)){
			extension=extension.toLowerCase();
			//If there is not mimetype, we will try to find it in the internal table mimetype based on the extension
			String query="SELECT mimetype FROM MimeType WHERE extension='"+extension+"'";
			IList queryList = ics.SQL("MimeType",query,null,-1,true, new StringBuffer());
			if (queryList != null && queryList.hasData()) {
				mimeType=queryList.getValue("mimetype");
			}else{
				mimeType="";
			}
		}
		
		%><blobservice:getidcolumn varname="blobIdColumn"/><blobservice:gettablename varname="blobTableName"/><blobservice:geturlcolumn varname="blobURLName"/><%

		String blobTable = ics.GetVar ( "blobTableName" );
		String blobUrlCol = ics.GetVar ( "blobURLName" );
			
		if (fileName != null) {				
			if (fileName.indexOf(",") != -1) {
				fileName = fileName.substring(0,fileName.lastIndexOf(","));
			}
			fileName = fileName.concat(".");		
			ics.SetVar("fileName", fileName);
		}

		String contentDisposition = null;
		
		if (Utilities.goodString(mimeType)){

			if (mimeType.lastIndexOf("image") == -1){
				if ("application/pdf".equals(mimeType)) {
					contentDisposition = "attachment; filename=" + fileName + extension;

				}else if (mimeType.lastIndexOf("flash") != -1){
					contentDisposition = "inline; filename=" + fileName + extension;
				}else {
					contentDisposition = "attachment; filename=" + fileName + extension;
				}
			} // end if no image
		} // end if mimeType
		
		// Append file extention if preferredFilename attribute is used
		if (Utilities.goodString(preferredFilename) && Utilities.goodString(extension)){
			preferredFilename=preferredFilename+ "." + extension;
		}
		%><render:getbloburl 
			satellite="true"
			outstr='<%=outputVar%>'
				blobtable='<%=blobTable%>'
				blobkey="id"
				blobwhere='<%=blobFieldId%>'
				blobcol='<%=blobUrlCol%>'			
				blobheader='<%=mimeType%>'
				preferredfile='<%=preferredFilename%>'
				preferreddir='<%=preferredDir%>'><% 
				if (Utilities.goodString(mimeType)){
					if (mimeType.lastIndexOf("flash") != -1){
						%><render:argument name="blobheadername1" value="content-type"/><%
						%><render:argument name="blobheadervalue1" value="<%= mimeType %>"/><%
						%><render:argument name="blobheadername2" value="Content-Disposition"/><%
						%><render:argument name="blobheadervalue2" value="<%= contentDisposition %>"/><%
					}else{
						%><render:argument name="blobheadername1" value="Content-Disposition"/><%
						%><render:argument name="blobheadervalue1" value="<%= contentDisposition %>"/><%
					}
				}
				else if (contentDisposition != null) {
					%><render:argument name="blobheadername1" value="Content-Disposition"/><%
					%><render:argument name="blobheadervalue1" value="<%= contentDisposition %>"/><%
					
				}
				
					
		%></render:getbloburl><%
	}// end if (stWhere.lastIndexOf(".") != -1)
} // end if ( Utilities.goodString( blobFieldId ) && Utilities.goodString( outputVar ) )
	
ics.RemoveVar("FlexBlobOutputVar");
ics.RemoveVar("mimeType");
ics.RemoveVar("FlexBlobId");
    
%></cs:ftcs>